<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tags extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();

	}

	// If $postId is NULL, gets all posts, otherwise get single post from db 
	// returns $post[]
	public function get_post_tag($postId)
	{
		$post=array();
		if ($postId !== null){		
			$query = $this->db->get_where('post_tag', array('id' => $postId));
			if ($query->num_rows() == 1) {	
				foreach($query->result() as $row){
				$post['id'] = $row->id;
				$post['name'] = $row->name;
				}
				return $post;
			}	
		} else {
			$query = $this->db->get('post_tag');
            if ($query->num_rows() !== 0 ){
                foreach($query->result() as $row){
                    $post['id'][] = $row->id;
                    $post['name'][] = $row->name;
                }
                return $post;
            }
			
		}		
	}

	function insert_post($data){
		$this->db->insert('posts', $data);
		return;
	}

	function update_post($postId,$data){
		$id = $postId;
       	$this->db->where('id',$id);
		$this->db->update('posts', $data);
		return;
	}

	function delete_post($postId){
		$id = $postId;		
       		$this->db->delete('posts',array('id' => $id));
		return;
	}
}
